var http = require('../../utils/http.js')
var util = require('../../utils/util.js')
var localstorage = require('../../utils/localstorage.js')
var appInstance = getApp()
var page = 1;
var pageSize = 3;

Page({

  /**
   * 页面的初始数据
   */
  data: {

    initCar: {
      id: 0,
      title: "所有品牌"
    },
    initSubCar: {
      id: 0,
      title: "所有车型"
    },
    initXiLie: {
      xilieId: 0,
      title: "所有系列"
    },

    selectCar: {
      id: 0,
      title: "所有品牌"
    },
    selectSubCar: {
      id: 0,
      title: "所有车型"
    },
    selectXiLie: {
      xilieId: 0,
      title: "所有系列"
    },
    CarArr: [],
    SubCarArr: [],
    XiLieArr: [],
    //
    datalist: [],
    isMore: true,
    searchWork: "",
    //
    startX: [],
    s: [],
    lefts: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getdatalist(
    //   this.data.selectCar,
    //   this.data.selectXiLie,
    //   this.data.selectZhigan,
    //   this.data.selsectColor,
    //   ""
    // );

    this.getCarlist();

    page = 1;
    this.getdatalist(
      this.data.selectCar,
      this.data.selectSubCar,
      this.data.selectXiLie
    );

  },




  getCarlist() {
    var _this = this;

    var postdata = {
      "Page": 1,
      "PageSize": 9999,
      "pid": 0
    }

    http.request({
      url: 'car_carServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;

        wx.stopPullDownRefresh()

        _this.setData({
          CarArr: [...[_this.data.selectCar], ...newArr],
          SubCarArr: [_this.data.selectSubCar]
        })


      }
    })
  },

  getSubCarlist(pid) {
    var _this = this;

    var postdata = {
      "Page": 1,
      "PageSize": 9999,
      "pid": pid
    }
  
     var defcar={
      id: 0,
      title: "所有车型"
    };
    http.request({
      url: 'car_carServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;

        wx.stopPullDownRefresh()
       
        _this.setData({
          SubCarArr:  [...[defcar], ...newArr],
        })


      }
    })
  },



  bindPickerChangeZG: function (e) {
    var index = e.detail.value
    var sdata = this.data.ZhiganArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectZhigan: sdata
    })
    page = 1;
    this.data.datalist = []
    this.getdatalist(
      this.data.selectCar,
      this.data.selectXiLie,
      sdata,
      this.data.selsectColor,
      this.data.searchWork
    );
  },

  bindPickerChangeColor: function (e) {
    var index = e.detail.value
    var sdata = this.data.ColorArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selsectColor: sdata
    })
    page = 1;
    this.data.datalist = []
    this.getdatalist(
      this.data.selectCar,
      this.data.selectXiLie,
      this.data.selectZhigan,
      sdata,
      this.data.searchWork
    );
  },

  bindPickerChangeCar: function (e) {

    var index = e.detail.value

    var sdata = this.data.CarArr[index]
  
    this.setData({
      selectCar: sdata,
      SubCarArr:[{
        id: 0,
        title: "所有车型"
      }],
      selectSubCar:{
        id: 0,
        title: "所有车型"
      }
    })
    if(sdata==undefined)
    {
      return;
    }
    if (sdata.id == 0) {
      return;
    }

    this.getSubCarlist(sdata.id)

    // page = 1;
    // this.data.datalist = []
    // this.getdatalist(
    //   sdata,
    //   this.data.selectSubCar,
    //   this.data.selectXiLie);
  },

  bindPickerChangeCar2: function (e) {

    var index = e.detail.value
    var sdata = this.data.SubCarArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectSubCar: sdata,
      datalist:[],
    })
    this.getdatalist(
      this.data.selectCar,
      sdata,
      this.data.selectXiLie);
  },

  bindPickerChangeXilie: function (e) {
    var index = e.detail.value
    var sdata = this.data.XiLieArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectXiLie: sdata
    })
    page = 1;
    this.data.datalist = []
    this.getdatalist(
      this.data.selectCar,
      this.data.selectSubCar,
      sdata);
  },


  onReachBottom() {
    if (this.data.isMore) {
      page = page + 1;
      this.getdatalist(
        this.data.selectCar,
        this.data.selectSubCar,
        this.data.selectXiLie
      );
    }
  },

  onPullDownRefresh()
  {
    this.setData({
      datalist: [],
    })
    page = 1;
    this.getdatalist(
      this.data.selectCar,
      this.data.selectSubCar,
      this.data.selectXiLie
    );

  },


  getdatalist: function (selectCar, selectSubCar, selectXiLie) {
    var _this = this;
    this.setData({
      isloading:true
    })

    var postdata = {
      "Page": page,
      "PageSize": pageSize,
      "carid": selectCar.id,
      "carSubId": selectSubCar.id,
      "xilieId": selectXiLie.xilieId,
      "isRandom":(selectCar.id+selectSubCar.id+selectXiLie.xilieId)==0
    }
    http.request({
      url: 'car_360ServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {

        let newArr = res.result.data.items;
        let totalPages = res.result.data.totalPages
        let xilieArr = res.result.xilies

        wx.stopPullDownRefresh()

        _this.setData({
          isloading:false,
          datalist: [..._this.data.datalist, ...newArr],
          XiLieArr: [...[_this.data.initXiLie], ...xilieArr],
          isMore: page < totalPages,

        })


      }
    })
  },


  onclickItem: function (e) {
    // console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/case/details?id=' + id + '&title=' + title,
    })
  },


  touchstart: function (e) {
    var changeindex = e.currentTarget.dataset.index;
    let updateTodo = `startX[${changeindex}]`

    this.setData({
      [updateTodo]: e.changedTouches[0].clientX
    })
  },
  tous(e) {
    var changeindex = e.currentTarget.dataset.index;
    let updateTodoS = `s[${changeindex}]`
    let updateTodolefts = `lefts[${changeindex}]`

    if (e.changedTouches[0].clientX < this.data.startX[changeindex]) {

      let w = this.data.s[changeindex] += 1
      if (w < 38) {
        this.setData({
          [updateTodoS]: w
        })
        let left = this.data.s[changeindex] * 750
        this.setData({
          [updateTodolefts]: left
        })
      } else {
        let ws = 750 * 37
        this.setData({
          [updateTodoS]: 0,
          [updateTodolefts]: 0
        })
      }


    } else {
      if (this.data.s[changeindex] > 0) {
        let w = this.data.s[changeindex] -= 1
        this.setData({
          [updateTodoS]: w
        })
        let left = w * 750
        this.setData({
          [updateTodolefts]: left
        })
      } else {
        let ws = 750 * 37
        this.setData({
          [updateTodoS]: 37,
          [updateTodolefts]: ws
        })
      }

    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})